define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Stardetail, $stateParams, $window, GoPage, $sce, GoShare,GoWapAuth, $timeout) {
		$scope.detail_id = $stateParams.detail_id;
		//一键分享
		$scope.isHide = true; //默认隐藏
		//动态设置高度
		var all = $window.innerHeight;
		//计算滚动条
		$scope.myObj = {
			"height": all - 44 + "px"
		};
		//页面离开，分享关闭
		$scope.$on('$ionicView.afterLeave', function() {
			$scope.closeGuidShare();
		}, false);
		//显示弹窗
		$scope.show_apply = function() {
			if($scope.openGuidShare()){
				$scope.isHide = false;
			}
		}
		$scope.hide_apply = function() {
			$scope.isHide = true;
		}
		//加载数据
		$scope.init_data = function() {
			Stardetail.get_detail($scope.detail_id, function(data) {
				$scope.detailData = data;
				$scope.initVideo(data.video);
				$scope.go_share_wx();
			});

		};
		$scope.init_data();
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", { store_id: store_id });
		};
		//视频播放初始化
		$scope.initVideo = function(item) {
			if(item.video_classification == "source") {
				//1.判断url的后缀 否则使用iframe
				$scope.myURL = $sce.trustAsResourceUrl($scope.getIsThird(item.video_source));
			} else {
                $scope.myURL = $sce.trustAsResourceUrl(item.video_url);
			}
			//视频播放不了提示用户
			$timeout(function() {
				var av = document.getElementById('abcde')
				if(document.getElementById('abcde') != null) {
					if(document.getElementById('abcde').contentWindow.document) {
						var video = document.getElementById('abcde').contentWindow.document.body;
						var div = document.createElement("div");
						if($scope.detailData.video.video_source)
							div.innerText = '视频无法播放(链接不是通用链接)';
						else
							div.innerText = '视频未设置播放链接';
						div.setAttribute('style', 'text-align: center;margin-top: 6rem;')
						video.appendChild(div);
					}
				}

			}, 2000);

		};
		$scope.getIsThird = function(url) {
			//iframe模式
			if(url.lastIndexOf('<iframe') == 0 && url.lastIndexOf('</iframe>') > 0) {
				var video = document.createElement("div");
				video.innerHTML = url;
				var ifreame = video.childNodes[0];
				return ifreame.getAttribute("src");
			} else {
				return url;
			}
		};
		//视频分享
		$scope.go_share = function(index) {
			var param = new Object();
			if($scope.detailData) {
				param.title = $scope.detailData.title ? $scope.detailData.title : undefined;
				param.desc = $scope.detailData.company_profile ? $scope.detailData.company_profile : undefined;
				param.thumb = $scope.detailData.video_pic ? $scope.detailData.video_pic : undefined;
				param.link = 'stardetail/' + $scope.detail_id;
			}
			GoShare.do_share(param, index);
		}
		//分享（自定义每个页面的参数）
		$scope.go_share_wx = function() {
			//分享打开
			GoWapAuth.execisoff(false);
			//设置分享
			var param = new Object();
			if($scope.detailData) {
				param.title = $scope.detailData.title ? $scope.detailData.title : undefined;
				param.desc = $scope.detailData.company_profile ? $scope.detailData.company_profile : undefined;
				param.thumb = $scope.detailData.video_pic ? $scope.detailData.video_pic : undefined;
				param.link = 'stardetail/' + $scope.detail_id;
			}
			GoWapAuth.execshare(param);
		}
		
	};

	controller.$inject = ['$scope', 'Stardetail', '$stateParams', '$window', 'GoPage', '$sce', 'GoShare','GoWapAuth', '$timeout'];

	return app.registerController("stardetailctrl", controller);
});